package org.chengsen.zhang;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.chengsen.zhang.util.ChineseCalendar;

/**
 * Hello world。.hello
 * 
 */
public class App {

	public static void main(String[] args) throws InterruptedException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException {
		testReflection();
	}

	public static void testReflection() throws ClassNotFoundException,
			InstantiationException, IllegalAccessException {
		Set set = (Set) Class.forName("java.util.HashSet").newInstance();
		System.out.println(set);
	}

	public static void testNextInt() {
		for (int i = 0; i < 15; i++) {
			System.out.println(new Random().nextInt(10));
		}
	}

	public static void testDecimal() {
		// 新建 BigDecimal
		BigDecimal bigDecimal = new BigDecimal("12.10");
		// 加运算
		BigDecimal addDecimal = bigDecimal.add(new BigDecimal(12.10f));
		// 除运算
		BigDecimal divideDecimal = addDecimal.divide(new BigDecimal(12));
		System.out.println(bigDecimal);

		// 注意 Double 和 float 在Java 中的表示形式
		System.out.println("addDecimal : " + addDecimal);
		System.out.println("divideDecimal : " + divideDecimal);
		System.out.println("addDecimal.doubleValue() : "
				+ addDecimal.doubleValue());
		System.out.println("divideDecimal.intValue() : "
				+ divideDecimal.intValue());

		MathContext content = new MathContext(4);
		// negate四舍五入 setScale设置精度
		System.out.println(addDecimal.negate(content).setScale(6));

		DecimalFormat format = new DecimalFormat();
		format.applyPattern("#0.0000");
		System.out.println(format.format(divideDecimal));
		// 建立百分比格式化引用 DecimalFormat 是NumberFormat 的子类
		NumberFormat percent = DecimalFormat.getPercentInstance();
		System.out.println("divideDecimal : " + divideDecimal);
		System.out.println(percent.format(divideDecimal));
	}

	public static void testFor() {
		for (int i = 0; i < 5; ++i) {
			System.out.println(i);
		}
	}

	public static void testList() {
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		System.out.println(list);
		System.out.println(list.toArray());

	}

	public static void testClone() {
		Test test = new Test();
		try {
			test.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void testToString() {
		System.out.print(new Test());
	}

	public static void testFload() {
		System.out.println("float :" + Float.floatToIntBits(1.22333f));
		System.out.println("double :" + Double.doubleToLongBits(1.22333));
	}

	public static void testHashCode() {
		Map<Test, String> m = new HashMap<Test, String>();
		Test test = new Test();
		m.put(new Test(), "hello");
		m.put(new Test(), "world");
		m.put(test, "nihao");
		m.put(test, "nihaoma");

		System.out.println(m.size());
	}

	public static void testCalendar() {
		ChineseCalendar chineseCalendar = new ChineseCalendar();
		System.out.println("农历" + chineseCalendar.getChineseDateString());
		System.out.println("公历"
				+ chineseCalendar.getSimpleGregorianDateString());
		System.out.println("" + chineseCalendar.getSimpleChineseDateString());
	}
}

class RunThread implements Runnable {

	public void run() {
		System.out.println(Thread.currentThread());
		try {
			Thread.sleep((long) (Math.random() * 1000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("runnable class");

	}
}

class MyThread extends Thread {
	private String name;

	public MyThread() {

	}

	public MyThread(String name) {
		this.name = name;
	}

	@Override
	public void run() {
		System.out.println(Thread.currentThread());
		for (int i = 0; i < 5; i++) {
			try {
				this.sleep((long) (1000 * Math.random()));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(name + "运行     " + i);
		}
	}

}

class Test implements Cloneable {

	private final String name = "hello";
	private final Integer age = 29;

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	public int hasCode() {
		return 1;
	}
}
